home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / cli / UnixUtils.lha / UnixUtils / Source / tail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  3.1 KB  |  136 lines

  1. /*-------------------------------------------------*
  2.  | tail.c - Unix-like utility.                     |
  3.  | Syntax: tail [-N | +N] [file [file [ ... ] ]    |
  4.  | Prints on stdout the last N (default: N_DEFVAL) |
  5.  | lines read from the given files, or from stdin. |
  6.  | If +N is given, the first N lines of input will |
  7.  | be skipped, and the remainder printed.          |
  8.  | v1.00 MLO 911224                                |
  9.  *-------------------------------------------------*/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include "mlo.h"
  15.  
  16. #define N_DEFVAL      10
  17. #define LINE_LENGTH   128
  18.  
  19. Boolean skip = False;
  20. char *pBuffer, *pOver;
  21.  
  22. void DoTheStuff(FILE *fp, int n);
  23. void Syntax(void);
  24.  
  25. void main(
  26.   int argc,
  27.   char **argv
  28. ){
  29.   int nLines = N_DEFVAL;
  30.   int bufferLength;
  31.  
  32.   while (--argc) {
  33.     if ( ((*++argv)[0] == '-') ) {
  34.       if (isdigit((*argv)[1])) {
  35.         if ((nLines = atoi(*argv+1)) < 1) Syntax();
  36.       } else {
  37.         Syntax();
  38.       }
  39.     } else if ((*argv)[0] == '+') {
  40.       skip = True;
  41.       if (isdigit((*argv)[1])) {
  42.         if ((nLines = atoi(*argv+1)) < 1) Syntax();
  43.       } else if ((*argv)[1] == '\0') {
  44.         continue;
  45.       } else {
  46.         Syntax();
  47.       }
  48.     } else if ((*argv)[0] == '?') {
  49.       Syntax();
  50.     } else {
  51.       break;
  52.     }
  53.   }
  54.  
  55.   if (skip) {
  56.     if ((pBuffer = malloc(LINE_LENGTH)) == NULL) {
  57.       puts("tail: no memory.");
  58.       exit(SYS_ABORT_CODE);
  59.     }
  60.   } else {
  61.     if ((pBuffer = malloc(bufferLength = (nLines+1)*LINE_LENGTH)) == NULL) {
  62.       printf("tail: no memory for %d lines %d characters each.\n",
  63.              nLines, LINE_LENGTH);
  64.       exit(SYS_ABORT_CODE);
  65.     }
  66.     pOver = pBuffer + bufferLength;
  67.   }
  68.  
  69.   if (argc) {
  70.     while (argc--) {
  71.       FILE *fp;
  72.  
  73.       if ((fp = fopen(*argv, "r")) == NULL) {
  74.         printf("tail: couldn't open file \"%s\".\n", *argv);
  75.       } else {
  76.         DoTheStuff(fp, nLines);
  77.         fclose(fp);
  78.       }
  79.       ++argv;
  80.     }
  81.   } else {
  82.     DoTheStuff(stdin, nLines);
  83.   }
  84.  
  85.   free(pBuffer);
  86.   exit(SYS_NORMAL_CODE);
  87. }
  88.  
  89. void DoTheStuff(
  90.   FILE *fp,
  91.   int n
  92. ){
  93.   if (skip) {
  94.     while (n--) {
  95.       if (fgets(pBuffer, LINE_LENGTH, fp) == NULL) {
  96.         puts("END-OF-FILE");
  97.         return;
  98.       }
  99.     }
  100.     while (fgets(pBuffer, LINE_LENGTH, fp)) fputs(pBuffer, stdout);
  101.   } else {
  102.     char *pDest = pBuffer;
  103.     Boolean bufferFull = False;
  104.     char *pc;
  105.  
  106.     while (fgets(pDest, LINE_LENGTH, fp)) {
  107.       if ((pDest += LINE_LENGTH) == pOver) {
  108.         pDest = pBuffer;
  109.         bufferFull = True;
  110.       }
  111.     }
  112.  
  113.     if (bufferFull) {
  114.       pc = pDest;
  115.       do {
  116.         if ((pc += LINE_LENGTH) == pOver) pc = pBuffer;
  117.         fputs(pc, stdout);
  118.       } while (pc != pDest);
  119.     } else {
  120.       puts("TOP-OF-FILE");
  121.       for (pc=pBuffer; pc<pDest; pc+=LINE_LENGTH) fputs(pc, stdout);
  122.     }
  123.   }
  124. }
  125.  
  126. void Syntax(void)
  127. {
  128.   puts("\nUsage:   tail [-N | +N] [file [file [ ... ]]]");
  129.   printf("Purpose: prints on stdout the last N (default: %d) lines read\n",
  130.          N_DEFVAL);
  131.   puts("         from the given file(s), or from stdin. With +N, skips");
  132.   puts("         the first N input lines and prints the remainder.\n");
  133.  
  134.   exit(SYS_NORMAL_CODE);
  135. }
  136.